package org.example.leetcode;


/**
 * @author: lynn
 * @Descript:
 * @date: 2021/8/5 8:56
 * @version: 1.0
 */
public class LC48 {

    public static void main(String[] args) {
        int[][] matrix1={{1,2,3},{4,5,6},{7,8,9}};
        int[][] matrix2={{1,2,3},{4,5,6},{7,8,9}};
        LC48 lc=new LC48();
        lc.rotate(matrix1);
        lc.reRotate(matrix2);
        System.out.println("xxxxx");

    }
    //fixme https://www.bilibili.com/video/BV1GQ4y1P7wc?from=search&seid=14640184130471736729
    //fixme 顺时针， 左上右下对角线对称， 然后是中间对角线对称

    /**    原来  左上右下对角线对称后 \   中轴对称(逆序每一行)
     *    1 2 3      1 4 7              7 4 1
     *    4 5 6      2 5 8              8 5 2
     *    7 8 9      3 6 9              9 6 3
    **/
    public void rotate(int[][] matrix) { //fixme 顺时针
        if (matrix==null || matrix.length==0 || matrix[0]==null || matrix[0].length==0) return;
        //左下右上对角线对称
        for (int i = 0; i <matrix.length ; i++) {
            for (int j = 0; j <matrix[0].length; j++) {
                if(i>j){ //fixme  可以填 i<j 或 i>j,代表 \
                    int tmp=matrix[i][j];
                    matrix[i][j]= matrix[j][i];
                    matrix[j][i]=tmp;
                }
            }
        }

        for (int i=0;i<matrix.length;i++){
            for (int j = 0; j <matrix[i].length/2 ; j++) {
                int tmp=matrix[i][j];
                matrix[i][j]=matrix[i][matrix[i].length-1-j];
                matrix[i][matrix[i].length-1-j]=tmp;
            }
        }
    }

    //fixme 逆时针

    /**    原来  右上左下对角线对称后 /   中轴对称(逆序每一行)
     *    1 2 3      1 4 7              7 4 1
     *    4 5 6      2 5 8              8 5 2
     *    7 8 9      3 6 9              9 6 3
     **/
    public void reRotate(int[][] matrix) {// fixme 逆时针
        if (matrix==null || matrix.length==0 || matrix[0]==null || matrix[0].length==0) return;
        //左下右上对角线对称
        for (int i = 0; i <matrix.length ; i++) {
            for (int j = 0; j <matrix[0].length; j++) {
                if(i<matrix[i].length-j-1){ //fixme ,代表 /
                    int tmp=matrix[i][j];
                    matrix[i][j]= matrix[matrix[i].length-j-1][matrix.length-i-1];
                    matrix[matrix[i].length-j-1][matrix.length-i-1]=tmp;
                }
            }
        }
        for (int i=0;i<matrix.length;i++){
            for (int j = 0; j <matrix[i].length/2 ; j++) {
                int tmp=matrix[i][j];
                matrix[i][j]=matrix[i][matrix[i].length-1-j];
                matrix[i][matrix[i].length-1-j]=tmp;
            }
        }
    }
}
